﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dao.Intermediarios
{
    public class IPProveedorComp : Intermediario
    {
        public override List<T> getAll<T>()
        {
            return (from pr in dao.Proveedor_Componentes orderby pr.IdProveedor select pr).ToList() as List<T>;
        }

        public override List<T> getByCriterio<T>(string att, string op, string val)
        {
            List<T> listita = dao.ExecuteQuery<Proveedor_Componente>("SELECT * FROM [Proveedor-Componente] WHERE [Proveedor-Componente]." + att + " " + op + " '" + val + "'").ToList() as List<T>;  //+ "'AND Proveedor.fechaBaja IS NULL ORDER BY Proveedor.razonSocial;").ToList() as List<T>;

            return listita;
        }

        public override void save<T>(T entity)
        {
            try
            {
                Proveedor_MP pr = entity as Proveedor_MP;
                // pr.Activo = true;
                dao.Proveedor_MPs.InsertOnSubmit(pr);

            }
            catch (Exception ex)
            {
                throw new Exception("IPRProveedorMP.save - " + ex.Message);
            }
        }

        public void update<T>(T entity)
        {
            try
            {
                Proveedor_MP pr = entity as Proveedor_MP;
                dao.Proveedor_MPs.InsertOnSubmit(pr);
            }
            catch (Exception ex)
            {
                throw new Exception("IPRProveedor_MP.update - " + ex.Message);
            }
        }

        public override IList<T> getCriterioById<T>(string att, string op, int val)
        {
            return (from pc in dao.Proveedor_Componentes where pc.IdProveedor == Convert.ToInt16(val) select pc).ToList() as List<T>;
           // throw new NotImplementedException();
        }

        public override object getPorId(int id)
        {
            throw new NotImplementedException();
        }

        public override IList<T> getPorCriterio<T>(string name)
        {
            throw new NotImplementedException();
        }

        public override void Update<T>(T entity)
        {
            try
            {
                Proveedor pr = entity as Proveedor;
                dao.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("IPRProveedor.Update - " + ex.Message);
            }
        }

        public override void Delete(int valor)
        {
            string dt = DateTime.Now.Date.ToShortDateString();
            dao.ExecuteQuery<Proveedor>("UPDATE Proveedor SET fechaBaja = " + dt + " " + "WHERE Id = " + valor);
        }


    }
}
